﻿using System;


class Matrix
{
    //Exercise 1.
    //Write a program that fills and prints a matrix of size (n, n) as shown below: (examples for n = 4)

    static void Main()
    {

        //Declare the matrix
        Console.Write("Enter the size of the matrix: ");
        int n = int.Parse(Console.ReadLine());

        int[,] matrix = new int[n, n];

        //Matrix A
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                matrix[row, col] = (row + 1) + (n * col);
            }
        }
        Console.WriteLine("Matrix A: ");
        PrintMatrix(matrix);

        //Matrix B
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                if (col % 2 == 0)
                {
                    matrix[row, col] = (row + 1) + (n * col);
                }
                else
                {
                    matrix[row, col] = (n * (col + 1)) - row;
                }                
            }
        }
        Console.WriteLine("Matrix B: ");
        PrintMatrix(matrix);

        //Matrix C
        int count = 0;
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col <= row; col++)
            {
                matrix[n - row + col - 1, col] = count + 1;
                count++;
            }
        }

        for (int row = n - 2; row >= 0; row--)
        {
            for (int col = row; col >= 0; col--)
            {
                matrix[row - col, n - col - 1] = count+ 1;
                count++;
            }
        }
        Console.WriteLine("Matrix C: ");
        PrintMatrix(matrix);

        //Matrix D
        int counter = 1;
        int maxRow = n - 1;
        int maxCol = n - 1;
        int rows = 0;
        int columns = 0;

        while (counter <= n * n)
        {

            for (int i = rows; i <= maxRow; i++)    // 1-st colomn
            {
                matrix[i, columns] = counter;
                counter++;
            }
            columns++;                          // go to next colomn

            for (int i = columns; i <= maxCol; i++)     // last row
            {
                matrix[maxRow, i] = counter;
                counter++;
            }
            maxRow--;

            for (int i = maxRow; i >= rows; i--)            //last colomn
            {
                matrix[i, maxCol] = counter;
                counter++;
            }
            maxCol--;

            for (int i = maxCol; i >= columns; i--)             // 1-st row
            {
                matrix[rows, i] = counter;
                counter++;
            }
            rows++;                                             //next cicle should start from the next row
        }

        Console.WriteLine("Matrix D*: ");
        PrintMatrix(matrix);
    }

    private static void PrintMatrix(int[,] matrix)
    {
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                Console.Write("{0,3}", matrix[row, col]);
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }
}

